<template>
  <div class="WorkingSchedule" v-loading="loading">
    <el-form ref="form" :model="form" label-position="top" class="myform">
      <div>
        <el-form-item label="加工单号/采购单号" class="myform-item">
          <el-input
            v-model="form.processing_code"
            placeholder="请输入加工单号/采购单号"
            @keyup.enter.native="onSearch"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-top: 50px" class="myform-item">
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </div>
      <div>
        <el-form-item style="margin-top: 50px" class="myform-item">
          <el-button type="primary" @click="onCreate()">新建</el-button>
        </el-form-item>
      </div>
    </el-form>
    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="生产加工单号" align="center" prop="processing_code"></el-table-column>
      <el-table-column label="采购单号" align="center" prop="po_id"></el-table-column>
      <el-table-column label="款号" align="center" prop="type_no"></el-table-column>
      <el-table-column label="工厂" align="center" prop="factory_name"></el-table-column>
      <el-table-column label="大货成衣成本加价总价" align="center" prop="contract_machining_price"></el-table-column>
      <el-table-column label="实付成衣成本加价总价" align="center" prop="pay_machining_price"></el-table-column>

      <el-table-column label="数量" align="center" prop="num">
        <template slot-scope="{ row }">
          <div style="color:red">-{{ row.num }}</div>
        </template>
      </el-table-column>
      <el-table-column label="返修退款金额" align="center" prop="amount">
        <template slot-scope="{ row }">
          <div style="color:red">-{{ row.amount }}</div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="k_status">
        <template slot-scope="{ row }">
          <div :style="{
            color: row.k_status == 2 ? '#67C23A' : '',
          }">
            {{
              row.k_status == 0
                ? "未抵扣"
                : row.k_status == 2
                  ? "已抵扣"
                  : ""
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createtime"></el-table-column>
    </el-table>
    <pagination
      :form="form"
      :total="total"
      @paginationPostOrderTaskList="postAdminPricingGetRepairRefundList"
    />
    <!-- 违约金/赔偿金 -->
    <el-dialog
      title="新建返修退款"
      :visible.sync="penaltydialogVisible"
      :lose-on-click-modal="false"
      width="30%"
    >
      <el-form
        :model="penaltyForm"
        :rules="rulespenaltyForm"
        ref="penaltyForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="加工单号" prop="processing_code">
          <el-input v-model="penaltyForm.processing_code" placeholder="请输入加工单号"></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="penaltyForm.num" type="number" placeholder="请输入数量"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="penaltydialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onCreateAffirm('penaltyForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  postAdminPricingGetRepairRefundList,
  postAdminPricingEditRepairRefund,
} from "@/api";
import pagination from "@/components/pagination.vue";
export default {
  components: { pagination },
  data() {
    return {
      loading: false,
      penaltydialogVisible: false, // 违约金/赔偿金
      form: {
        processing_code: "",
        page: 1,
        size: 10
      },
      tableData: [],
      total: 1,
      // 新建
      penaltyForm: {
        processing_code: "",
        num: ""
      },
      rulespenaltyForm: {
        num: [
          { required: true, message: "请输入数量", trigger: "blur" },
        ],
        processing_code: [
          { required: true, message: "请输入生产加工单号", trigger: "blur" },
        ],
      },
    };
  },
  activated() {
    let form = this.form;
    this.postAdminPricingGetRepairRefundList(form)
  },
  methods: {
    //   新增
    onCreate(e) {
      this.penaltydialogVisible = true;
    },
    // 新增返修
    onCreateAffirm(formName) {
      let that = this;
      let obj = this.penaltyForm;
      let form = this.form;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          postAdminPricingEditRepairRefund(obj).then((res) => {
            that.postAdminPricingGetRepairRefundList(form);
            that.$message({
              type: "success",
              message: `返修退款创建成功!`,
            });
            that.penaltydialogVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 采购列表
    async postAdminPricingGetRepairRefundList(data) {
      this.loading = true;
      this.form = data;
      postAdminPricingGetRepairRefundList(data)
        .then((res) => {
          this.loading = false;
          console.log("采购列表", res);
          this.tableData = res.data;
          this.total = Number(res.total);
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 搜索
    onSearch() {
      this.form.page = 1;
      this.form.size = 10;
      this.postAdminPricingGetRepairRefundList(this.form);
    },
  },
};
</script>

<style lang="scss" scoped>
.myform {
  display: flex;
  justify-content: space-between;
  > div {
    display: flex;
  }
  &-item {
    margin-right: 10px;
  }
}
.WorkingSchedule {
  background: #ffff;
  padding: 20px;
  border-radius: 2px;
  @include boxShadow;
}
</style>
